package com.distributed.kv.entity;

import org.springframework.data.cassandra.core.mapping.PrimaryKey;
import org.springframework.data.cassandra.core.mapping.Table;
import org.springframework.data.cassandra.core.mapping.Column;
import java.time.Instant;
import java.util.Objects;

/**
 * KV存储实体类
 * 
 * Cassandra表设计:
 * - 使用key作为主键,确保唯一性和快速查找
 * - 按key哈希分布到不同节点,实现负载均衡
 * - 支持TTL自动过期
 */
@Table("kv_pairs")
public class KvPair {
    
    @PrimaryKey
    private String key;
    
    @Column("id")
    private Long id;
    
    @Column("value")
    private String value;
    
    @Column("created_at")
    private Instant createdAt;
    
    @Column("updated_at") 
    private Instant updatedAt;
    
    @Column("access_count")
    private Long accessCount;
    
    @Column("ttl")
    private Integer ttl; // TTL in seconds
    
    public KvPair() {
        this.createdAt = Instant.now();
        this.updatedAt = Instant.now();
        this.accessCount = 0L;
    }
    
    public KvPair(String key, Long id) {
        this();
        this.key = key;
        this.id = id;
    }
    
    public KvPair(String key, Long id, String value) {
        this(key, id);
        this.value = value;
    }
    
    // Getters and Setters
    public String getKey() {
        return key;
    }
    
    public void setKey(String key) {
        this.key = key;
    }
    
    public Long getId() {
        return id;
    }
    
    public void setId(Long id) {
        this.id = id;
    }
    
    public String getValue() {
        return value;
    }
    
    public void setValue(String value) {
        this.value = value;
        this.updatedAt = Instant.now();
    }
    
    public Instant getCreatedAt() {
        return createdAt;
    }
    
    public void setCreatedAt(Instant createdAt) {
        this.createdAt = createdAt;
    }
    
    public Instant getUpdatedAt() {
        return updatedAt;
    }
    
    public void setUpdatedAt(Instant updatedAt) {
        this.updatedAt = updatedAt;
    }
    
    public Long getAccessCount() {
        return accessCount;
    }
    
    public void setAccessCount(Long accessCount) {
        this.accessCount = accessCount;
    }
    
    public void incrementAccessCount() {
        this.accessCount = (this.accessCount == null ? 0 : this.accessCount) + 1;
        this.updatedAt = Instant.now();
    }
    
    public Integer getTtl() {
        return ttl;
    }
    
    public void setTtl(Integer ttl) {
        this.ttl = ttl;
    }
    
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        KvPair kvPair = (KvPair) o;
        return Objects.equals(key, kvPair.key);
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(key);
    }
    
    @Override
    public String toString() {
        return "KvPair{" +
                "key='" + key + '\'' +
                ", id=" + id +
                ", value='" + value + '\'' +
                ", createdAt=" + createdAt +
                ", updatedAt=" + updatedAt +
                ", accessCount=" + accessCount +
                ", ttl=" + ttl +
                '}';
    }
}